/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVGH;

import DAO.CTHoaDonDao;
import DAO.CTPXuatKhoDao;
import DAO.HoaDonDao;
import DAO.KhachHangDao;
import DAO.NhanVienDao;
import DAO.PXuatKhoDao;
import DAO.SanPhamDao;
import GUI.Var;
import POJO.CTHoaDon;
import POJO.CTPXuatKho;
import POJO.HoaDon;
import POJO.KhachHang;
import POJO.NhanVien;
import POJO.PXuatKho;
import POJO.SanPham;
import POJO.SoLuongXuat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kim - Khôi
 */
public class frLapPhieuXuatKho extends javax.swing.JInternalFrame {

	private DefaultTableModel tbmodel1;
	private DefaultTableModel tbmodel2;
	private DefaultTableModel tbmodel3;
	private HoaDon HD;
	private PXuatKho PXNew;
	private ArrayList<CTPXuatKho> CTX;
	private int MCX;	//ma san pham dang focus can xuat
	private int SLDX;	// so luong san pham da xuat
	private int SLCX;	// sô luong can xuat
	private String TCX;	//ten san pham can xuat

	/**
	 * Creates new form frLapPhieuXuatKho
	 */
	public frLapPhieuXuatKho() {
		initComponents();
		ngayXuat();
		hienThiNhanVienDangNhap();
		//	timHoaDon();
		//	loadThongTinHoaDon();

	}

	private void ngayXuat() {
		txNgayXuat.setText(Var.NGAY.get(Calendar.DATE) + "-" + ((Integer) Var.NGAY.get(Calendar.MONTH) + 1) + "-" + Var.NGAY.get(Calendar.YEAR));

	}

	private void hienThiNhanVienDangNhap() {
		txNVDN.setText(Var.NV.getTenNV());
	}

	private int maSanPhamDaTonTraiTrongChiTietXuat(int msp) {
		int tmp = -1;
		if (CTX == null) {
			return tmp;
		}
		for (int i = 0; i < CTX.size(); i++) {
			if (msp == CTX.get(i).getMaSP()) {
				return i;
			}
		}
		return tmp;
	}

	private Date getNgayXuat() {
		int[] d = new int[3];
		String st = txNgayXuat.getText();
		String[] s = st.split("-");
		d[0] = Integer.parseInt(s[0]);
		d[1] = Integer.parseInt(s[1]);
		d[2] = Integer.parseInt(s[2]);

		Date dt = new Date(d[2], d[1] - 1, d[0]);// co 12 thang , thang dau tien bat dau = 0
		int nv = Var.NV.getMaNV();
		return dt;
	}

	public String KiemTraMa() {
		String str = "";
		if (txMaHoaDon.getText().equals("")) {
			str += "- Chưa nhập mã hoá đơn ! \r\n";
		}
		if (txMaHoaDon.getText().equals("0")) {
			str += "- Không nhập mã hoá đơn bằng 0 ! \r\n";
		}
		return str;
	}

	public void DefaultTable1() {
		tbmodel1 = new DefaultTableModel();
		//tạo các cột
		this.tbmodel1.addColumn("STT ");
		this.tbmodel1.addColumn("Mã sản phẩm");
		this.tbmodel1.addColumn("Sản phẩm");
		this.tbmodel1.addColumn("Số lượng");
		this.tbmodel1.addColumn("Tổng số đã xuất");
		tbChiTietHoaDon.setModel(tbmodel1);


	}

	private void DefaultTable2() {
		tbmodel2 = new DefaultTableModel();
		this.tbmodel2.addColumn("STT ");
		this.tbmodel2.addColumn("Số phiếu xuất");
		this.tbmodel2.addColumn("Ngày");
		tbPhieuXuat.setModel(tbmodel2);

	}

	private void DefaultTable3() {
		tbmodel3 = new DefaultTableModel();
		this.tbmodel3.addColumn("STT");
		this.tbmodel3.addColumn("Mã sản phẩm");
		this.tbmodel3.addColumn("Tên SP");
		this.tbmodel3.addColumn("Số lượng");
		tbCTPX.setModel(tbmodel3);

	}

	private void capNhatSoLuongDaBanChoModel() {
		ArrayList<SoLuongXuat> dssl = null;

		PXuatKho px = new PXuatKho(0, Var.NV.getMaNV(), HD.getMaHD());
		try {
			dssl = PXuatKhoDao.layDanhSachSoLuongDaXuat(px);

		} catch (Exception e) {
		}
		for (int i = 0; i < tbmodel1.getRowCount(); i++) {
			int maSP = (Integer) tbmodel1.getValueAt(i, 1);
			for (int j = 0; j < dssl.size(); j++) {
				if (dssl.get(j).getMaSanPham() == maSP) {
					tbmodel1.setValueAt(dssl.get(j).getSoLuong(), i, 4);
					break;
				}
			}
		}
	}

	private void loadCTHD() {
		DefaultTable1();
		ArrayList<CTHoaDon> dsct = null;
		try {
			dsct = CTHoaDonDao.layDSByMaHD(HD.getMaHD());
		} catch (Exception e) {
		}
		SanPhamDao dao = new SanPhamDao();
		int i = 1;
		for (CTHoaDon cthd : dsct) {
			int maSP = cthd.getMaSP();
			SanPham sp = dao.layDanhSachByMaSP(maSP);
			Object[] items = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				cthd.getSoLuong(),
				0
			};
			i++;
			tbmodel1.addRow(items);
		}
		tbChiTietHoaDon.setModel(tbmodel1);

		capNhatSoLuongDaBanChoModel();
	}

	private void khoaKhongChoXuat() {
		btChonXuat.setEnabled(false);
		btXuatAll.setEnabled(false);
		btLuu.setEnabled(false);
	}

	private void moKhoaChoXuat() {
		btChonXuat.setEnabled(true);
		btXuatAll.setEnabled(true);

	}

	public void loadThongTinHoaDon() {

		if (HD == null) {
			DefaultTable1();
			DefaultTable2();
			DefaultTable3();
			txTenKhachHang.setText("");
			txTrangThai.setText("");
			txNVBan.setText("");
			return;

		}
		if (HD.getTrangThai() == 1) {
			txTrangThai.setText("Chưa thanh toán");
			khoaKhongChoXuat();
		} else if (HD.getTrangThai() == 2) {
			txTrangThai.setText("Đã thanh toán - Chưa giao.");
			moKhoaChoXuat();

		} else if (HD.getTrangThai() == 3) {
			txTrangThai.setText("Hoàn tất");
			khoaKhongChoXuat();
		}

		//load khach hang cua hoa don
		KhachHang kh = new KhachHang();
		try {
			kh = KhachHangDao.layByMa(HD.getMaKH());
		} catch (Exception e) {
		}
		txTenKhachHang.setText(kh.getTenKH());
		// load nhan vien ban
		NhanVien nv = new NhanVien();
		try {
			nv = NhanVienDao.layByMa(HD.getMaNVPhuTrach());
		} catch (Exception e) {
		}
		txNVBan.setText(nv.getTenNV());

		// load chi tiet hoad don
		loadCTHD();
		loadCacPhieuXuatCuaHoaDon();
	}

	private void loadCacPhieuXuatCuaHoaDon() {
		DefaultTable2();
		ArrayList<PXuatKho> ds = null;
		try {
			ds = PXuatKhoDao.layByMaHD(HD.getMaHD());
		} catch (Exception e) {
		}
		int i = 1;
		for (PXuatKho px : ds) {
			Object[] items = new Object[]{
				i,
				px.getMaPXK(),
				Var.hienThiNgayVN(px.getNgayXuat())
			};
			i++;
			tbmodel2.addRow(items);
		}
		int soDongPX = tbmodel2.getRowCount();
		if (soDongPX > 0) {
			int mapx = (Integer) tbmodel2.getValueAt(soDongPX - 1, 1);
			if (mapx != 0) {
				loadChiTietPhieuXuat(mapx);
			}
		} else {
			DefaultTable3();// xoa tb CTPX
		}
	}

	private void loadChiTietPhieuXuat(int maPX) {
		DefaultTable3();
		ArrayList<CTPXuatKho> ds = null;
		try {
			ds = CTPXuatKhoDao.layDanhSachByMaPX(maPX);
		} catch (Exception e) {
		}
		int i = 1;
		SanPhamDao dao = new SanPhamDao();
		SanPham sp = new SanPham();
		for (CTPXuatKho px : ds) {
			sp = dao.LaySanPhamByMa(px.getMaSP());
			Object[] items = new Object[]{
				i,
				px.getMaSP(),
				sp.getTenSP(),
				px.getSoLuong()
			};
			i++;
			tbmodel3.addRow(items);
		}
	}

	public void LoadPXK(int maHD) {
		ArrayList<PXuatKho> dsPXuatKho = PXuatKhoDao.layByMaHD(maHD);
		int i = 1;
		for (PXuatKho pxk : dsPXuatKho) {
			Object[] items = new Object[]{
				i,
				pxk.getMaPXK(),
				pxk.getNgayXuat(),};
			i++;
			if (txNgayXuat.getText() == "Đã thanh toán - Chưa giao hàng" || txNgayXuat.getText() == "Đã thanh toán và giao hàng - hoàn tất") {
				txNgayXuat.setText(String.valueOf(pxk.getNgayXuat()));
			} else {
				Calendar now = Calendar.getInstance();
				txNgayXuat.setText(now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE));
			}
//            LoadChiTietPXK(pxk.getMaPXK());
		}

	}
//    public void LoadChiTietPXK(int maPXK)
//    {
//        ArrayList<CTPXuatKho> dsCTPXuatKho = CTPXuatKhoDao.layByMaPXK(maPXK); 
//        SanPhamDao dao = new SanPhamDao();
//        int i = 1;
//        for(CTPXuatKho ctpxk: dsCTPXuatKho) {
//            int maSP = ctpxk.getMaSP();
//            SanPham sp = dao.layDanhSachByMaSP(maSP);
//            Object [] items = new Object [] {
//                i,
//                sp.getTenSP()
//            };    
//            Object [] items2 = new Object [] {
//                ctpxk.getSoLuong()                
//            };    
//            i++;                       
//            tbmodel3.addRow(items);  
//            tbmodel3.addColumn("Số lượng",items2);  
//        }                
//    }

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel6 = new javax.swing.JPanel();
              jPanel2 = new javax.swing.JPanel();
              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              tbChiTietHoaDon = new javax.swing.JTable();
              jLabel9 = new javax.swing.JLabel();
              jPanel9 = new javax.swing.JPanel();
              btXuatAll = new javax.swing.JButton();
              btChonXuat = new javax.swing.JButton();
              btLuu = new javax.swing.JButton();
              btIn = new javax.swing.JButton();
              btHuy = new javax.swing.JButton();
              btDong = new javax.swing.JButton();
              btXoa = new javax.swing.JButton();
              jPanel5 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              jLabel4 = new javax.swing.JLabel();
              txTrangThai = new javax.swing.JTextField();
              txTenKhachHang = new javax.swing.JTextField();
              txMaHoaDon = new javax.swing.JTextField();
              jbtnTim = new javax.swing.JButton();
              jLabel7 = new javax.swing.JLabel();
              txNgayXuat = new javax.swing.JTextField();
              jLabel6 = new javax.swing.JLabel();
              txNVDN = new javax.swing.JTextField();
              jLabel5 = new javax.swing.JLabel();
              txNVBan = new javax.swing.JTextField();
              jPanel8 = new javax.swing.JPanel();
              jPanel7 = new javax.swing.JPanel();
              jScrollPane4 = new javax.swing.JScrollPane();
              tbCTPX = new javax.swing.JTable();
              jLabel8 = new javax.swing.JLabel();
              jScrollPane3 = new javax.swing.JScrollPane();
              tbPhieuXuat = new javax.swing.JTable();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("LẬP PHIẾU XUẤT KHO");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jLabel1)
                            .addGap(0, 0, 0))
              );

              jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn hóa đơn"));

              jPanel4.setToolTipText("");

              tbChiTietHoaDon.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã sản phẩm", "Sản phẩm", "Số lượng", "Tống số đã xuất"
                     }
              ) {
                     Class[] types = new Class [] {
                            java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class
                     };

                     public Class getColumnClass(int columnIndex) {
                            return types [columnIndex];
                     }
              });
              tbChiTietHoaDon.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            tbChiTietHoaDonMouseClicked(evt);
                     }
              });
              jScrollPane1.setViewportView(tbChiTietHoaDon);

              jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel9.setText("Chi tiết hóa đơn");

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addGap(0, 0, 0))
                     .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addGap(0, 0, 0))
                     .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                   .addGap(20, 20, 20)
                                   .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                                   .addGap(0, 0, 0)))
              );

              btXuatAll.setText(">>");
              btXuatAll.setToolTipText("Xuất hết cho hóa đơn");
              btXuatAll.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btXuatAllActionPerformed(evt);
                     }
              });

              btChonXuat.setText(">");
              btChonXuat.setToolTipText("Xuất sản phẩm đang chọn");
              btChonXuat.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btChonXuatActionPerformed(evt);
                     }
              });

              btLuu.setText("Lưu");
              btLuu.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btLuuActionPerformed(evt);
                     }
              });

              btIn.setText("In");
              btIn.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btInActionPerformed(evt);
                     }
              });

              btHuy.setText("<<");
              btHuy.setToolTipText("Hủy không xuất nữa");
              btHuy.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btHuyActionPerformed(evt);
                     }
              });

              btDong.setText("Đóng");
              btDong.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btDongActionPerformed(evt);
                     }
              });

              btXoa.setText("<");

              javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
              jPanel9.setLayout(jPanel9Layout);
              jPanel9Layout.setHorizontalGroup(
                     jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel9Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(btXuatAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btChonXuat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btLuu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btIn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btHuy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(btDong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
              );
              jPanel9Layout.setVerticalGroup(
                     jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel9Layout.createSequentialGroup()
                            .addGap(11, 11, 11)
                            .addComponent(btXuatAll)
                            .addGap(6, 6, 6)
                            .addComponent(btChonXuat)
                            .addGap(60, 60, 60)
                            .addComponent(btLuu)
                            .addGap(6, 6, 6)
                            .addComponent(btIn)
                            .addGap(64, 64, 64)
                            .addComponent(btXoa)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btHuy)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                            .addComponent(btDong)
                            .addContainerGap())
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );

              jLabel2.setText("Mã hóa đơn:");

              jLabel3.setText("Tên khách hàng:");

              jLabel4.setText("Tình trạng:");

              txTrangThai.setEditable(false);
              txTrangThai.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

              txTenKhachHang.setEnabled(false);

              jbtnTim.setText("Tìm");
              jbtnTim.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnTimActionPerformed(evt);
                     }
              });

              jLabel7.setText("Ngày xuất:");

              txNgayXuat.setEnabled(false);

              jLabel6.setText("NV đăng nhập:");

              txNVDN.setEnabled(false);

              jLabel5.setText("Nhân viên bán:");

              txNVBan.setEnabled(false);

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addComponent(txMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(jbtnTim, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                   .addComponent(txTenKhachHang)
                                   .addComponent(txTrangThai))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(txNgayXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                   .addComponent(txNVDN)
                                   .addComponent(txNVBan))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addGap(1, 1, 1)
                                          .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                 .addComponent(txMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                 .addComponent(jLabel2)
                                                 .addComponent(jbtnTim)))
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(txNgayXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addComponent(jLabel7)))
                            .addGap(6, 6, 6)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(txTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addComponent(jLabel3))
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addGap(1, 1, 1)
                                          .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                 .addComponent(txNVDN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                 .addComponent(jLabel6))))
                            .addGap(5, 5, 5)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(txTrangThai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addComponent(jLabel4))
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addGap(5, 5, 5)
                                          .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                 .addComponent(txNVBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                 .addComponent(jLabel5))))
                            .addGap(0, 4, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách phiếu xuất"));

              tbCTPX.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã SP", "Tên SP", "Số lượng"
                     }
              ));
              tbCTPX.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            tbCTPXMouseClicked(evt);
                     }
              });
              jScrollPane4.setViewportView(tbCTPX);

              jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel8.setText("Chi tiết xuất");

              javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
              jPanel7.setLayout(jPanel7Layout);
              jPanel7Layout.setHorizontalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                     .addGroup(jPanel7Layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addGap(0, 0, Short.MAX_VALUE))
              );
              jPanel7Layout.setVerticalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
              );

              tbPhieuXuat.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Số phiếu xuất", "Ngày"
                     }
              ));
              tbPhieuXuat.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            tbPhieuXuatMouseClicked(evt);
                     }
              });
              jScrollPane3.setViewportView(tbPhieuXuat);

              javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
              jPanel8.setLayout(jPanel8Layout);
              jPanel8Layout.setHorizontalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                     .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel8Layout.setVerticalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel8Layout.createSequentialGroup()
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel6Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private void timHoaDon() {
		HD = new HoaDon();
		HD.setMaHD(Integer.parseInt(txMaHoaDon.getText().trim()));
		HoaDonDao dao = new HoaDonDao();
		try {
			HD = dao.layByMa(HD.getMaHD());

		} catch (Exception e) {
		}
		loadThongTinHoaDon();

	}
       private void tbCTPXMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbCTPXMouseClicked
		int dong = this.tbCTPX.getSelectedRow();
		String ma = this.tbCTPX.getValueAt(dong, 1).toString();
		String soluong = this.tbCTPX.getValueAt(dong, 2).toString();
		//     this.jtxtMaSP.setText(ma);
		//     this.jtxtSoLuong.setText(soluong);
       }//GEN-LAST:event_tbCTPXMouseClicked

       private void tbPhieuXuatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPhieuXuatMouseClicked
		// TODO add your handling code here:
		if (CTX != null) {
			return;
		}
		int dong = tbPhieuXuat.getSelectedRow();
		if (dong != -1) {
			int mapx = (Integer) tbmodel2.getValueAt(dong, 1);
			loadChiTietPhieuXuat(mapx);

		} else {
			DefaultTable3();// xoa het CT Phieu xuat
		}
       }//GEN-LAST:event_tbPhieuXuatMouseClicked

	private boolean kiemTraMNhapMaHoaDon() {
		try {
			Integer.parseInt(txMaHoaDon.getText().trim());
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập sai số giá. Nhập lại:", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txMaHoaDon.selectAll();
			txMaHoaDon.requestFocus();
			return true;
		}
		return false;
	}
       private void jbtnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimActionPerformed
		// TODO add your handling code here:
		this.MCX = 0;
		this.SLCX = 0;
		if (kiemTraMNhapMaHoaDon()) {
			return;
		}
		huyKhongXuat();
		timHoaDon();

       }//GEN-LAST:event_jbtnTimActionPerformed
	private void capNhatSoLuongDaXuatThem(int MSP, int soLuongDaXuat) {
		int msp;
		for (int i = 0; i < tbmodel1.getRowCount(); i++) {
			msp = (Integer) tbmodel1.getValueAt(i, 1);
			if (msp == MSP) {
				soLuongDaXuat = soLuongDaXuat + (Integer) tbmodel1.getValueAt(i, 4);
				tbmodel1.setValueAt(soLuongDaXuat, i, 4);
				return;
			}
		}
	}

	private void taoMoiPhieuXuat() {
		if (PXNew == null) {

			PXNew = new PXuatKho();
			PXNew.setMaPXK(-1);
			PXNew.setMaNVPhuTrach(Var.NV.getMaNV());
			PXNew.setMaHD(HD.getMaHD());
			PXNew.setNgayXuat(this.getNgayXuat());

			// xoa CTPX de chuan bi them vao 
			DefaultTable3();
		}

		newCTX();// new chi tiet phieu xuat
	}

	private void themPhieuXuatMoiVaoModel() {
		if (PXNew.getMaPXK() == -1) {
			PXNew.setMaPXK(0);

			Object[] items = new Object[]{
				(tbmodel2.getRowCount() + 1),
				PXNew.getMaPXK(),
				Var.hienThiNgayVN(PXNew.getNgayXuat())
			};
			tbmodel2.addRow(items);
		}
	}

	private int laySoLuongDaXuat(int maSanPham) {
		int tmp = -1;
		for (int i = 0; i < tbmodel1.getRowCount(); i++) {
			if ((Integer) tbmodel1.getValueAt(i, 1) == maSanPham) {
				return (Integer) tbmodel1.getValueAt(i, 4);
			}
		}
		return tmp;
	}

	private void newCTX() {
		if (CTX == null) {
			CTX = new ArrayList<CTPXuatKho>();
		}
	}

	private void themCTPhieuXuat(int maSanPham, int soLuongCanXuat, int slx) {
		if (maSanPham == 0) {
			return;
		}
		int soLuongDaXuat = laySoLuongDaXuat(maSanPham);
		while (slx == 0) {
			String strSL = JOptionPane.showInputDialog("Nhấp số lượng muốn xuất");
			try {
				slx = Math.abs(Integer.parseInt(strSL.trim()));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "Nhâp số", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
				slx = 0;
			}
		}
		slx = soLuongCanXuat < (soLuongDaXuat + slx) ? soLuongCanXuat - soLuongDaXuat : slx;

		soLuongDaXuat = soLuongDaXuat + slx;
		if (slx != 0) {

			themChiTietPhieuXuatKhongKiemTra(maSanPham, slx);
		}

	}

	private void themChiTietPhieuXuatKhongKiemTra(int maSanPham, int slx) {
		capNhatSoLuongDaXuatThem(maSanPham, slx);

		int vt = maSanPhamDaTonTraiTrongChiTietXuat(maSanPham);
		if (vt == -1) {
			CTPXuatKho ctn = new CTPXuatKho(0, slx, maSanPham, 0);
			CTX.add(ctn);
			Object[] cti = new Object[]{tbmodel3.getRowCount() + 1, ctn.getMaSP(), getTenSanPhamCanXuat(maSanPham), ctn.getSoLuong()};
			tbmodel3.addRow(cti);
		} else {
			int x = CTX.get(vt).getSoLuong() + slx;
			CTX.get(vt).setSoLuong(x);
			tbmodel3.setValueAt(x, vt, 3);
		}
		btLuu.setEnabled(true);
	}

	private String getTenSanPhamCanXuat(int masp) {
		String tmp = "";
		for (int i = 0; i < tbmodel1.getRowCount(); i++) {
			if ((Integer) tbmodel1.getValueAt(i, 1) == masp) {
				return (String) tbmodel1.getValueAt(i, 2);
			}
		}
		return tmp;
	}

	private ArrayList<SoLuongXuat> laySanPhamDeXuatHet() {
		ArrayList<SoLuongXuat> tmp;

		tmp = new ArrayList<SoLuongXuat>();
		for (int i = 0; i < tbChiTietHoaDon.getRowCount(); i++) {
			if ((Integer) tbChiTietHoaDon.getValueAt(i, 3) > (Integer) tbChiTietHoaDon.getValueAt(i, 4)) {
				SoLuongXuat vt = new SoLuongXuat();
				vt.setMaSanPham((Integer) tbChiTietHoaDon.getValueAt(i, 1));
				vt.setSoLuong((Integer) tbChiTietHoaDon.getValueAt(i, 3) - (Integer) tbChiTietHoaDon.getValueAt(i, 4));
				tmp.add(vt);
			}

		}
		return tmp;
	}

	private void xuatHetChoHoaDon() {
		ArrayList<SoLuongXuat> tmp;
		tmp = laySanPhamDeXuatHet();
		for (int i = 0; i < tmp.size(); i++) {
			int masp = tmp.get(i).getMaSanPham();
			int soLuongXuat = tmp.get(i).getSoLuong();
			themChiTietPhieuXuatKhongKiemTra(masp, soLuongXuat);

		}
	}
       private void tbChiTietHoaDonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbChiTietHoaDonMouseClicked
		// TODO add your handling code here:

		if (HD.getTrangThai() != 2) {
			khoaKhongChoXuat();
			return;
		}
		int dong = tbChiTietHoaDon.getSelectedRow();
		int msp = 0;
		int slCanXuat = 0;
		int slDaXuat = 0;
		if (dong != - 1) {
			msp = (Integer) tbmodel1.getValueAt(dong, 1);

			slCanXuat = (Integer) tbmodel1.getValueAt(dong, 3);
			slDaXuat = laySoLuongDaXuat(msp);
		}
		if (slCanXuat == slDaXuat || slCanXuat == -1) {
			khoaKhongChoXuat();
		} else {
			moKhoaChoXuat();
		}
		this.MCX = msp;
		this.SLCX = slCanXuat;
       }//GEN-LAST:event_tbChiTietHoaDonMouseClicked

       private void btXuatAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXuatAllActionPerformed
		// TODO add your handling code here:
		if (tbmodel1.getRowCount() == 0) {
			return;
		} else {
			taoMoiPhieuXuat();
			xuatHetChoHoaDon();
			themPhieuXuatMoiVaoModel();
		}
       }//GEN-LAST:event_btXuatAllActionPerformed

       private void btChonXuatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btChonXuatActionPerformed
		// TODO add your handling code here:

		int dong = tbChiTietHoaDon.getSelectedRow();
		if (dong == -1) {// kiem tra them dieu kien co focus 
			JOptionPane.showMessageDialog(this, "Chọn chi tiết hóa đơn cần xuất");
			return;
		}
		taoMoiPhieuXuat();
		themCTPhieuXuat(this.MCX, this.SLCX, 0);
		themPhieuXuatMoiVaoModel();
		btHuy.setEnabled(true);
       }//GEN-LAST:event_btChonXuatActionPerformed

       private void btLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLuuActionPerformed
		// TODO add your handling code here:
		try {
			if (CTX == null) {
				return;
			}
			if (PXuatKhoDao.them(this.PXNew))// them phieu xuat
			{
				// cap nhat Ma Phiêu xuat cho chi tiet phieu xuat moi
				for (int i = 0; i < this.CTX.size(); i++) {
					this.CTX.get(i).setMaPXK(this.PXNew.getMaPXK());
				}

				// them chi tiet phieu xuat
				if (CTPXuatKhoDao.them(CTX)) {
					JOptionPane.showMessageDialog(this, "Đã xuất xong", "Thông báo thành công", JOptionPane.INFORMATION_MESSAGE);
					// cập nhật lại phiếu xuất trên lưới
					//cập nhật lại chi tiết phiếu xuất
					// giải phong PXNew
					// giải phóng Chi tiêt phiếu xuất
					huyKhongXuat();//da xuat xong giai phong vung nho chuan bi xuat nua
					timHoaDon();

				} else {
					JOptionPane.showMessageDialog(this, "Không thể thêm chi tiết phiếu xuất", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);

				}
			} else {
				JOptionPane.showMessageDialog(this, "Không thể thêm phiếu xuất", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
				return;
			}
		} catch (Exception e) {
		}
       }//GEN-LAST:event_btLuuActionPerformed

       private void btInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_btInActionPerformed

       private void btHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHuyActionPerformed
		// TODO add your handling code here:
		huyKhongXuat();
		timHoaDon();
       }//GEN-LAST:event_btHuyActionPerformed

       private void btDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDongActionPerformed
		// TODO add your handling code here:
		this.dispose();

       }//GEN-LAST:event_btDongActionPerformed
	private void huyKhongXuat() {
		btLuu.setEnabled(false);

		CTX = null;
		PXNew = null;
	}
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton btChonXuat;
       private javax.swing.JButton btDong;
       private javax.swing.JButton btHuy;
       private javax.swing.JButton btIn;
       private javax.swing.JButton btLuu;
       private javax.swing.JButton btXoa;
       private javax.swing.JButton btXuatAll;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JLabel jLabel9;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JPanel jPanel7;
       private javax.swing.JPanel jPanel8;
       private javax.swing.JPanel jPanel9;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JScrollPane jScrollPane3;
       private javax.swing.JScrollPane jScrollPane4;
       private javax.swing.JButton jbtnTim;
       private javax.swing.JTable tbCTPX;
       private javax.swing.JTable tbChiTietHoaDon;
       private javax.swing.JTable tbPhieuXuat;
       private javax.swing.JTextField txMaHoaDon;
       private javax.swing.JTextField txNVBan;
       private javax.swing.JTextField txNVDN;
       private javax.swing.JTextField txNgayXuat;
       private javax.swing.JTextField txTenKhachHang;
       private javax.swing.JTextField txTrangThai;
       // End of variables declaration//GEN-END:variables
}
